package com.jcl.recruit.api.crawler.controller;

import com.jcl.core.common.page.PageResult;
import com.jcl.core.resp.ApiResponse;
import com.jcl.core.utils.StringUtils;
import com.jcl.core.web.model.CrawlerUserToken;
import com.jcl.recruit.crawler.vo.CrawlerResumeCompleteParam;
import com.jcl.recruit.crawler.service.CrawlerResumeService;
import com.jcl.recruit.crawler.vo.CrawlerResumeDownloadListParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName CrawlerResumeDownloadController

 * @Author yyliu
 * @Date 2018/10/22 10:28
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/crawler")
public class CrawlerResumeDownloadController {

    private static final Logger logger = LoggerFactory.getLogger(CrawlerResumeDownloadController.class);
    @Autowired
    private CrawlerResumeService crawlerResumeService;

    @RequestMapping(value = "/resume/download/list", method = {RequestMethod.POST})
    public ApiResponse downloadResumeList(@RequestBody CrawlerResumeDownloadListParam param) {

        if (StringUtils.isEmpty(param.getCorpCode()) && StringUtils.isEmpty(param.getSource())) {

            return ApiResponse.error("param is lose !");
        }

        PageResult data = crawlerResumeService.downloadResumeList(param);

        return ApiResponse.success(data);
    }

    @RequestMapping(value = "/resume/complete", method = RequestMethod.POST)
    public ApiResponse completeResume(@RequestBody CrawlerResumeCompleteParam entity) {

        if (StringUtils.isEmpty(entity.getTenantId())) {
            return ApiResponse.error("param 'tenantId' is lose !");
        }
        CrawlerUserToken userToken = null;
        userToken = CrawlerUserToken.decodeTenantId(entity.getTenantId());
        logger.info("resume download complete message :");
        logger.info("{");
        logger.info("tenantId : {}", entity.getTenantId());
        if (null == userToken) {
            return ApiResponse.error("param 'tenantId' is error !");
        }
        String corpCode = userToken.getCorpCode();
        String userId = userToken.getUserId();
        String resumeId = userToken.getResumeId();
        logger.info("corpCode : {}", corpCode);
        logger.info("resumeId : {}", resumeId);
        logger.info("}");
        crawlerResumeService.completeResume(corpCode, userId, resumeId, entity);
        return ApiResponse.success("SUCCESS");
    }


}
